package sc.common.test.hopper.handler;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

import sc.common.test.TestUtil;

public class MyHandler extends SimpleChannelHandler {

	protected TestUtil TU;
	
	public MyHandler(TestUtil tu){
		TU = tu;
	}
	
	@Override
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		Channel ch = e.getChannel();
		TU.p("New Connect, local[%s], remote[%s]", ch.getLocalAddress(),
				ch.getRemoteAddress());
	}

	@Override
	public void channelBound(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		TU.p("Channel Bound, state[%s], value[%s]", e.getState(), e.getValue());
	}

	@Override
	public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		TU.p("Channel Open, state[%s], value[%s]", e.getState(), e.getValue());
	}

	@Override
	public void channelUnbound(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		TU.p("Channel Unbound, state[%s], value[%s]", e.getState(),
				e.getValue());
	}

	@Override
	public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		TU.p("Channel Closed, state[%s], value[%s]", e.getState(), e.getValue());
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
			throws Exception {
		e.getCause().printStackTrace();
		e.getChannel().close();
	}
}
